package com.squirrel.michale;

import java.util.PriorityQueue;

/**
 * @author guanhao 观浩
 * @version 1.0.0.0
 * @createTime 2023/3/2 7:46 AM
 * @company Michale Squirrel
 * @link
 * @description
 */
public class LCP0030 {

    public int magicTower(int[] nums) {
        int sum = 0;
        for (int i = 0; i < nums.length; i++) {
            sum += nums[i];
        }
        if (sum < 0) {
            return -1;
        }
        long sumBlood = 1;
        int resetCount = 0;
        PriorityQueue<Integer> monster = new PriorityQueue<>();
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] < 0) {
                monster.offer(nums[i]);
            }
            sumBlood += nums[i];
            if (sumBlood <= 0) {
                resetCount++;
                sumBlood -= monster.poll();
            }
        }
        return resetCount;

    }

    public int magicTower2(int[] nums) {

        int sum = 0;
        for (int i = 0; i < nums.length; i++) {
            sum += nums[i];
        }
        if (sum < 0) {
            return -1;
        }

        int firstBlood = 1;
        long sumBlood = firstBlood;
        int resetCount = 0;
        PriorityQueue<Integer> monster = new PriorityQueue<>();
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] < 0) {
                monster.offer(nums[i]);
            }
            sumBlood += nums[i];
            if (sumBlood <= 0) {
                resetCount++;
                if (!monster.isEmpty()) {
                    Integer poll = monster.poll();
                    sumBlood -= poll;
                }
            }
        }
        return resetCount;

    }
}
